﻿package ru.kohl.view.display{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.geom.*;
	import flash.filters.*;
	public class BackgroundComponent extends Sprite {
		public var temptxt:TextField;
		private var _texts_arr:Array;
		public var _mask:Sprite;
		private var _hit_sprite:Sprite
		public function BackgroundComponent():void {

			this.addEventListener(Event.ADDED_TO_STAGE,init);
			temptxt.visible=false;
		}


		private function init(e:Event):void {			
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			this.stage.addEventListener(Event.RESIZE,resizeHandler);
			createMask();
			createDiagonal();			
			createHitArea()
		}


		private function createMask():void {
			if (! _mask) {
				_mask=new Sprite  ;
				stage.addChild(_mask);
			}

			
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xFFCCCC);
			_mask.graphics.drawRect(5,3,this.stage.stageWidth-10,this.stage.stageHeight-100);
			_mask.graphics.endFill();
			this.mask=_mask;

		}



		private function createDiagonal():void {
			if (_texts_arr) {
				for (var i:int; i<_texts_arr.length; i++) {
					this.removeChild(_texts_arr[i]);
				}

			}
			_texts_arr=new Array  ;						
			
			var texts:Array=temptxt.text.split(" ");
			var metrics:TextLineMetrics=temptxt.getLineMetrics(0);
			var char_width:Number=Math.floor(metrics.width/temptxt.text.length);			
			var start_x:Number=stage.stageWidth*.5-temptxt.width*.5-char_width*5;
			var start_y:Number=(stage.stageHeight*.5)-62.5;
			
			var middle_text:TextField=createTextField();
			middle_text.x=start_x
			middle_text.y=start_y;
			_texts_arr.push(middle_text);			
			var top_text:TextField=createTextField();
			top_text.x=start_x;
			top_text.y=start_y-metrics.height;
			_texts_arr.push(top_text);
			start_y=top_text.y;			
			while(start_y>_mask.x){
				start_y-=metrics.height
				var tfild:TextField=createTextField();
				tfild.x=start_x;
				tfild.y=start_y;
				_texts_arr.push(tfild);
				
				}
				
				//----------------------------------------
				start_y=middle_text.y+metrics.height;
				
				var bottom_text:TextField=createTextField();
				bottom_text.y=start_y;
				bottom_text.x=start_x;
				_texts_arr.push(bottom_text);
				
				while(start_y<(stage.stageHeight-120)){
					start_y+=metrics.height;
					var b_field:TextField=createTextField();
					b_field.x=start_x;
					b_field.y=start_y;
					_texts_arr.push(b_field);
					
					
					}
			_texts_arr.sortOn("y",Array.NUMERIC);			
			var delta_x:Number=(this.stage.stageWidth+metrics.width*.5)/_texts_arr.length;
			var middle_index=getIndexBy(middle_text)
			for(var q:int; q<_texts_arr.length;q++){
				var textfield:TextField=_texts_arr[q]
				if(textfield !=middle_text){					
					textfield.x+=delta_x*(middle_index-q);
					}
				
				}
				
				
			var selected_text1:TextField=createTextField();
			var tf1:TextFormat=getTextFormat();
			selected_text1.text=texts[0]+String.fromCharCode(32);
			selected_text1.appendText(texts[1]);
			tf1.color=0x323538;
			selected_text1.setTextFormat(tf1);			
			selected_text1.x=top_text.x;
			selected_text1.y=top_text.y+metrics.height*.5;
			_texts_arr.push(selected_text1);
			selected_text1.filters=[getFilter()];
			
			var selected_text2=createTextField();
			selected_text2.text=texts[2];
			var tf2:TextFormat=getTextFormat();
			tf2.size=39;
			tf2.color=0x323538;
			selected_text2.setTextFormat(tf2);
			var point:Point=getTextPoint(texts[2],middle_text);
			selected_text2.x=point.x+middle_text.x-char_width*.5;
			selected_text2.y=point.y;
			selected_text2.filters=[getFilter()];
			_texts_arr.push(selected_text2);
			
			
			var selected_text3=createTextField();
			selected_text3.text=texts[3];
			var tf3:TextFormat=getTextFormat();
			tf3.color=0x323538;
			selected_text3.setTextFormat(tf3);
			var point2:Point=getTextPoint(texts[3],bottom_text);
			selected_text3.x=bottom_text.x+point2.x+char_width*1.5;
			selected_text3.y=point2.y+metrics.height*.35;
			selected_text3.filters=[getFilter()];
			_texts_arr.push(selected_text3);
			
			
		}
		
		
		private function getIndexBy(t:TextField):int{
			var index:int
			for(var i:int=0;i<_texts_arr.length;i++){
				if(_texts_arr[i]==t){
					index=i
					break;
					}
				
				
				}
		
			return index;
			}



		private function createTextField():TextField {
			var t:TextField=getTextField();
			t.text=temptxt.text;
			t.setTextFormat( getTextFormat());
			var endtf:TextFormat=new TextFormat  ;
			endtf.size=39;
			endtf.color=0xe6e6e6;
			t.setTextFormat(endtf,17,20);
			this.addChild(t);
			t.cacheAsBitmap=true;
			

			return t;
		}

		private function resizeHandler(e:Event):void {
			createMask();
			createDiagonal();
			createHitArea()
		}

		private function getTextFormat():TextFormat {
			var tf:TextFormat=new TextFormat  ;
			tf.size=25;
			tf.align="center"
			tf.color=0xd6d6d7;
			tf.font="AvantGarde Bk BT";
			return tf;



		}

		private function getTextField():TextField {
			var t:TextField=new TextField  ;
			t.background=false;
			t.selectable=false;		
			t.autoSize="left";
			t.embedFonts=true;
			t.antiAliasType=AntiAliasType.ADVANCED;
			t.sharpness=-8;
			t.thickness=100;


			return t;
		}



		private function getTextPoint(st:String,t:TextField):Point {
			var point:Point;
			var temp_text:String=t.text;
			var start_index:int=temp_text.search(st);
			var metrix:TextLineMetrics=t.getLineMetrics(0);
			return new Point(((metrix.width/temp_text.length)*start_index),t.y);
		}


		private function getFilter():GlowFilter {
			var filter:GlowFilter=new GlowFilter  ;
			filter.color=0xFFFFFF;
			filter.strength=3;
			filter.alpha=.8;
			filter.blurX=10;
			filter.blurY=10;
			filter.quality=BitmapFilterQuality.HIGH;
			filter.inner=false;


			return filter;
		}
		
		
		private function createHitArea():void{
			if(!_hit_sprite){
				_hit_sprite=new Sprite;
				addChild(_hit_sprite)
				
				
				
				}
		_hit_sprite.graphics.clear();
		_hit_sprite.graphics.beginFill(0xFFFCC,0)
		_hit_sprite.graphics.drawRect(_mask.x,_mask.y,_mask.width,_mask.height)
		_hit_sprite.graphics.endFill();
		_hit_sprite.buttonMode=true;
			
			
			
			
			}


	}

}